//
//  MYStatusPictureView.swift
//  NewMicroblog
//
//  Created by apple on 2016/10/28.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SDWebImage

private let pictureViewCellIdentifier = "pictureViewCellIdentifier"

// cell 间距
private let cellMargin: CGFloat = 5
// cell 宽高
private let cellWH = CGFloat(Int((SCREENWIDTH - 20 - cellMargin*2)/3))

class MYStatusPictureView: UICollectionView {
    
    var picUrls:[MYPictureInfoModel]?{
        didSet{
            countLabel.text = "\(picUrls?.count ?? 0)"
            conculateSize(count: picUrls?.count ?? 0)
            
            reloadData()
        }
    }
    func conculateSize(count:Int){
        if count == 1 {
            let key = picUrls?.first?.thumbnail_pic ?? ""
            let img = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: key)
            if img != nil {
                let res = img!.size
                self.snp_updateConstraints(closure: { (make) in
                    make.size.equalTo(res)
                })
                layoutIfNeeded()
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                layout.itemSize = res
                return
            }
        }
        let col = count >= 3 ? 3 : count
        let row = (count - 1)/3 + 1
        
        // 最终宽和高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*cellMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*cellMargin
        
        // 更新约束
        self.snp_updateConstraints(closure: { (make) in
            make.size.equalTo(CGSize(width: w, height: h))
        })
        
        layoutIfNeeded()
        // 设置 itemSize
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: cellWH, height: cellWH)

        
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
         let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumLineSpacing = cellMargin
        layout.minimumInteritemSpacing = cellMargin
        dataSource = self
        register(MYStatusPictrueViewCell.self, forCellWithReuseIdentifier: pictureViewCellIdentifier)
        addSubview(countLabel)
        countLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
    }
    private lazy var countLabel:UILabel = {
       let label = UILabel(title: "", fontSize: 35, textColor: UIColor.red)
        label.textAlignment = .center
        return label
        
    }()
    
    
    
}
extension MYStatusPictureView:UICollectionViewDataSource{
   
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: pictureViewCellIdentifier, for: indexPath) as! MYStatusPictrueViewCell
        cell.picUrl = picUrls![indexPath.item]
        return cell
    }
    
}
class MYStatusPictrueViewCell: UICollectionViewCell {
    
    var picUrl:MYPictureInfoModel?{
        didSet{
            backImgV.my_setImage(urlStr: picUrl?.thumbnail_pic, placeholderImgName: "timeline_image_placeholder")
        }
    }
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        contentView.addSubview(backImgV)
        backImgV.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    
    private lazy var backImgV:UIImageView = {
        let view = UIImageView(imageName: "timeline_image_placeholder")
        view.contentMode = .scaleAspectFill
        view.layer.masksToBounds = true
        return view
    }()
    
}
